package com.ay.weixin.autoreply.event.handle;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.apache.velocity.VelocityContext;
import org.lizar.config.Config;
import org.lizar.config.keys.Keys;
import org.lizar.controller.loader.EventLoader;
import org.lizar.ioc.Cell;
import org.lizar.ioc.Container;
import org.lizar.json.JObject;
import org.lizar.log.Log;
import org.lizar.log.Logger;
import org.lizar.util.StringHelper;
import org.lizar.util.VelocityHelper;

import com.ay.weixin.autoreply.db.IbatisAutoReplyImageDao;
import com.ay.weixin.autoreply.db.IbatisAutoReplyMusicDao;
import com.ay.weixin.autoreply.db.IbatisAutoReplyNewsDao;
import com.ay.weixin.autoreply.db.IbatisAutoReplyVideoDao;
import com.ay.weixin.autoreply.db.IbatisAutoReplyVoiceDao;
import com.ay.weixin.autoreply.domain.Message;
import com.ay.weixin.autoreply.domain.MsgImage;
import com.ay.weixin.autoreply.domain.MsgMusic;
import com.ay.weixin.autoreply.domain.MsgNews;
import com.ay.weixin.autoreply.domain.MsgVideo;
import com.ay.weixin.autoreply.domain.MsgVoice;
import com.ay.weixin.config.ProjectConfig;

public class HandleMsg implements Cell{

	Log log =Logger.newInstance(this.getClass());
	private HandleDuoKefu handle_dkf;
	private IbatisAutoReplyImageDao auto_msg_image_dao;
	private IbatisAutoReplyVoiceDao auto_msg_vocie_dao;
	private IbatisAutoReplyVideoDao auto_msg_video_dao;
	private IbatisAutoReplyMusicDao auto_msg_music_dao;
	private IbatisAutoReplyNewsDao auto_msg_news_dao;
	private HandleTextMsg handle_text;
	
	
	
	@Override
	public void init_property() throws Exception {
		handle_dkf=Container.get(HandleDuoKefu.class);
		auto_msg_image_dao=Container.get(IbatisAutoReplyImageDao.class);
		auto_msg_vocie_dao=Container.get(IbatisAutoReplyVoiceDao.class);
		auto_msg_video_dao=Container.get(IbatisAutoReplyVideoDao.class);
		auto_msg_music_dao=Container.get(IbatisAutoReplyMusicDao.class);
		auto_msg_news_dao=Container.get(IbatisAutoReplyNewsDao.class);
		handle_text=Container.get(HandleTextMsg.class);
		
		
		
		
	}
	
	public void handle_msg( JObject msg,EventLoader el,String echogetStr,long wxproject_id) throws IOException{
		if(StringHelper.isNull(msg.getStr("msg_type"))){
			log.error("System may get expression error as get msg type is null msg:"+msg);
			return;
		}
		
		if(ProjectConfig._bool(wxproject_id,"duokefu.open")&&StringHelper.equals(msg.getStr("content"), ProjectConfig.getStr(wxproject_id,"duokefu.transfer_key"))){
			try {
				handle_dkf.handle(el, msg);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return;
		}
		
		String msg_type=msg.getStr("msg_type");
		
		if(StringHelper.equals(msg_type, "text")){
			handle_text.handle_text_msg(el,wxproject_id,msg);
		}else if(StringHelper.equals(msg_type, "image")){
			handle_image_msg(el,msg);
		}else if(StringHelper.equals(msg_type, "voice")){
			handle_voice_msg(el,msg);
		}else if(StringHelper.equals(msg_type, "location")){
			handle_location_msg(el,msg);
		}else if(StringHelper.equals(msg_type, "link")){
			handle_link_msg(el,msg);
		}else{
			handle_text.handle_auto_reply(el,wxproject_id, msg);
			System.out.println("unknow type:"+msg_type);
		}
		
		
		
		//handle_default_event(el,msg.getStr("to_user_name"),msg.getStr("from_user_name"),msg.getStr("msg_type"));
		
	}
	
	
	
	private void handle_image_msg(EventLoader el,JObject msg){
		
		
	}

	private void handle_voice_msg(EventLoader el,JObject msg){
	
	
	}

	private void handle_location_msg(EventLoader el,JObject msg){
	
	
	}
	
	private void handle_link_msg(EventLoader el,JObject msg){
		
		
	}
	
	public void reply(long wxproject_id,Message reply_msg,EventLoader el,JObject msg) throws IOException{
		String msg_path=String.format(Keys.getStr("msg_template"), reply_msg.getResponse_type());
		String result=getMessageResult(wxproject_id,reply_msg,msg_path,el,msg.getStr("to_user_name"),msg.getStr("from_user_name"));
		el.text(result);
	}

	
	
public String getMessageResult(long wxproject_id,Message msg,String path,EventLoader el,String to,String from){
		
		if(StringHelper.equals(msg.getResponse_type(), "text")){
			return read_text_msg(path,el,to,from, msg.getContent());
		}else if(StringHelper.equals(msg.getResponse_type(), "image")){
			List<MsgImage> ilist=null;
			try {
				ilist=auto_msg_image_dao.getImageList(wxproject_id,msg.getId());
			} catch (SQLException e) {
				e.printStackTrace();
				return "";
			}
			return read_image_msg(path,el,to,from, ilist);
			
		}else if(StringHelper.equals(msg.getResponse_type(), "voice")){
			List<MsgVoice> ilist=null;
			try {
				ilist=auto_msg_vocie_dao.getVoiceList(wxproject_id,msg.getId());
			} catch (SQLException e) {
				e.printStackTrace();
				return "";
			}
			return read_voice_msg(path,el,to,from, ilist);
			
		}else if(StringHelper.equals(msg.getResponse_type(), "video")){
			List<MsgVideo> ilist=null;
			try {
				ilist=auto_msg_video_dao.getVideoList(wxproject_id,msg.getId());
			} catch (SQLException e) {
				e.printStackTrace();
				return "";
			}
			return read_vedio_msg(path,el,to,from, ilist);
			
		}else if(StringHelper.equals(msg.getResponse_type(), "music")){
			List<MsgMusic> ilist=null;
			try {
				ilist=auto_msg_music_dao.getMusicList(wxproject_id,msg.getId());
			} catch (SQLException e) {
				e.printStackTrace();
				return "";
			}
			return read_music_msg(path,el,to,from, ilist);
			
		}else if(StringHelper.equals(msg.getResponse_type(), "news")){
			System.out.println(msg.getId());
			List<MsgNews> ilist=null;
			try {
				ilist=auto_msg_news_dao.getNewsList(wxproject_id,msg.getId());
			} catch (SQLException e) {
				e.printStackTrace();
				return "";
			}
			return read_news_msg(path,el,to,from, ilist);
		}
		return "";
	}
	
public String read_news_msg(String path,EventLoader el,String to,String from,List<MsgNews> ilist){
	 // 取得velocity的上下文context
	  VelocityContext context = new VelocityContext();
	  // 把数据填入上下文
	  context.put("from_user_name", to);
	  context.put("to_user_name",from);
	  context.put("create_time", System.currentTimeMillis()/1000);
	  context.put("count",ilist==null?0:ilist.size());
	  context.put("root",Config.xpathgetStr("server_info.root"));
	  context.put("media_list", ilist);
	  
	 
	  String result=VelocityHelper.read_msg(el.getRealPath(path), context);
	  System.out.println(result);
	 return result;
}

public String read_music_msg(String path,EventLoader el,String to,String from,List<MsgMusic> ilist){
	 // 取得velocity的上下文context
	  VelocityContext context = new VelocityContext();
	  // 把数据填入上下文
	  context.put("from_user_name", from);
	  context.put("to_user_name",to);
	  context.put("create_time", System.currentTimeMillis()/1000);
	  context.put("media_list", ilist);
	  
	 return VelocityHelper.read_msg(el.getRealPath(path), context);
}

public String read_vedio_msg(String path,EventLoader el,String to,String from,List<MsgVideo> ilist){
	 // 取得velocity的上下文context
	  VelocityContext context = new VelocityContext();
	  // 把数据填入上下文
	  context.put("from_user_name", from);
	  context.put("to_user_name",to);
	  context.put("create_time", System.currentTimeMillis()/1000);
	  context.put("media_list", ilist);
	  
	 return VelocityHelper.read_msg(el.getRealPath(path), context);
}

public String read_text_msg(String path,EventLoader el,String to,String from,String content){
	 // 取得velocity的上下文context
	  VelocityContext context = new VelocityContext();
	  // 把数据填入上下文
	  context.put("from_user_name", from);
	  context.put("to_user_name",to);
	  context.put("create_time", System.currentTimeMillis()/1000);
	  context.put("content", content);
	  
	 return VelocityHelper.read_msg(el.getRealPath(path), context);
}

public String read_voice_msg(String path,EventLoader el,String to,String from,List<MsgVoice> ilist){
	 // 取得velocity的上下文context
	  VelocityContext context = new VelocityContext();
	  // 把数据填入上下文
	  context.put("from_user_name", from);
	  context.put("to_user_name",to);
	  context.put("create_time", System.currentTimeMillis()/1000);
	  context.put("media_list", ilist);
	  
	 return VelocityHelper.read_msg(el.getRealPath(path), context);
}

public String read_image_msg(String path,EventLoader el,String to,String from,List<MsgImage> ilist){
	 // 取得velocity的上下文context
	  VelocityContext context = new VelocityContext();
	  // 把数据填入上下文
	  context.put("from_user_name", from);
	  context.put("to_user_name",to);
	  context.put("create_time", System.currentTimeMillis()/1000);
	  context.put("media_list", ilist);
	  
	 return VelocityHelper.read_msg(el.getRealPath(path), context);
}

}
